﻿/* Project: Side Scrolling Shooter
 * The enemy parent class.
 * Can check  if it has collided with the player.
 * Last Update: 4/12/11,11:16PM
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TheFifthSpace
{
    public class Enemy : DrawableGameComponent
    {
        public int pointValue { get; set; }
        public float posX, posY;
        protected float dt;
        private const int maxOnscreenBullets = 15;

        // Only initialized by child classes:
        public Texture2D sprite { get; set; }
        public virtual Vector2 Offset { get; set; }

        protected float fireCountdown, speed;

        public Vector2 Position
        {
            get
            {
                return new Vector2(posX, posY);
            }
            set
            {
                posX = value.X;
                posY = value.Y;
            }
        }

        // CONSTRUCTOR
        public Enemy(Game g, Vector2 p)
            : base(g)
        {
            Game.Components.Add(this);
            Random r = new Random();
            Position = p;
            // Random delay before enemy's first attack
            fireCountdown = (float)(r.Next(130) + 230);
        }

        public void Die()
        {
            MainGame.Enemies.Remove(this);
            Game.Components.Remove(this);
        }

        public virtual void Fire()
        {
            // Implemented by child classes
        }

        // Check to see if ship has collided with player
        public virtual bool CollideWithPlayer(Rectangle pRect)
        {
            // Implemented by child classes, doesn't actually return false
            return false;
        }

        // Find vector between enemy and player
        public Vector2 RelativeToPlayer()
        {
            Vector2 r = MainGame.thePlayer.Position - this.Position;
            return r;
        }

        // UPDATE
        public override void Update(GameTime gameTime)
        {
            // Change in time
            dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
            posX += speed * dt; // Move accordingly

            // Firing bullets: delay time between fires
            if (fireCountdown > 0) fireCountdown--;
            else
            {
                if(MainGame.EnemyBullets.Count<=maxOnscreenBullets)     Fire();
                Random r = new Random();
                fireCountdown = (float)(1400 + r.Next(1800));
                // Intervals between attacks vary
            }

        }// END UPDATE

    }

}